<?php

class OrderManager_Module{

    private $orderId   = '';
    private $userId    = 0;


    function __construct(){}

    public function searchOrder(&$search, &$order, &$page, &$total, $eachpage = 10){

        $where = '1=1';
        $ordby = '';
        $_search = $_order = array();

        $key_equal = array('shop_id', 'user_id', 'status');
        $key_order = array('shop_id', 'status', 'create_dt');

        foreach($key_equal as $key){
            if(isset($search[$key]) && $search[$key]){
                $_search[$key]  = intval($search[$key]);
                $where .= " AND `{$key}` = '" .Db::escapeString($_search[$key]). "'";
            }
        }
        foreach($order as $key => $ord){
            if(in_array($key, $key_order)){
                $_order[]  = "`{$key}` " . (strtolower($ord) == 'desc' ? 'DESC' : 'ASC');
            }else{
                unset($order[$key]);
            }
        }
        if(isset($search['create_dt1']) && $search['create_dt1']){
            $_search['create_dt1'] = $search['create_dt1'];
            $where .= " AND `create_dt` >= '" .Db::escapeString($search['create_dt1']). "'";
        }
        if(isset($search['create_dt2']) && $search['create_dt2']){
            $_search['create_dt2'] = $search['create_dt2'];
            $where .= " AND `create_dt` <= '" .Db::escapeString($search['create_dt2']). "'";
        }

        $total = Db::getFirst("select count(order_id) from `biz_order` where {$where}");
        if($total > 0){
            if(count($_order)){
                $ordby = ' ORDER BY '. join(',', $_order);
            }
            $page = real_page($page, $total, $eachpage);
            $offset = ($page - 1) * $eachpage;
            $sql = "select * from `biz_order` where {$where} {$ordby} Limit {$offset}, {$eachpage}";
            return Db::getAll($sql);
        }else{
            $page = 1;
        }
        return false;
    }
    function getOrderDetail($order_id){
        $order_id = intval($order_id);
        return Db::getRow("SELECT * FROM `biz_order` WHERE `order_id` = $order_id");
    }
    function getOrderDish($order_id){
        return Db::getAll("SELECT * FROM `biz_waimai_order` WHERE `order_id` = $order_id");
    }

}
?>